<template>
  <div>
    <Header></Header>

    <div class="r-blogdetail">
      <h2 class="blog-title">{{ blog.title }}</h2>

      <el-link icon="el-icon-edit" v-show="isSelf">
        <router-link :to="{name:'BlogAdd',params:{blogId:blog.id}}">编辑</router-link>
      </el-link>
      <el-link class="r-icon-delete" icon="el-icon-delete" type="danger" v-show="isSelf" @click="blog_delete">
        删除
      </el-link>

      <el-divider></el-divider>
      <div class="blog-created">发表时间：{{ blog.created }}</div>

      <el-divider></el-divider>
      <div class="markdown-body" v-html="blog.content"></div>

    </div>


  </div>
</template>

<script>
import Header from "../components/Header";
import "github-markdown-css/github-markdown.css"

export default {
  name: "BlogDetail",
  components: {Header},
  data() {
    return {
      blog: {
        id: '',
        title: '',
        content: '',
        created: '',
      },
      isSelf: false,
    }
  },
  methods: {
    blog_delete() {
      var _this = this;
      this.$confirm('此操作将永久删除该博客, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(() => {
        //console.log("确定按钮触发")
        _this.$axios.get('/blog/delete/' + _this.blog.id, {
          headers: {
            "Authorization": localStorage.getItem('token')
          }
        }).then(res => {
          _this.$router.push({name: 'Blogs'})
        })
      }).catch(() => {
        //console.log("取消按钮触发")
      });
    }
  },
  created() {
    const _this = this;
    const blogId = this.$route.params.blogId;
    this.$axios.get('/blog/' + blogId).then(res => {
      let blog = res.data.data;
      _this.blog.id = blog.id;
      _this.blog.title = blog.title;
      _this.blog.created = blog.created;
      //渲染markdown格式
      var MarkdownIt = require("markdown-it");
      var md = new MarkdownIt();
      _this.blog.content = md.render(blog.content);

      //当博客id等于用户id时才展示
      _this.isSelf = (blog.userId === _this.$store.state.userInfo.id)
    })
  }
}
</script>

<style scoped>
.r-blogdetail {
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  width: 860px;
  min-height: 700px;
  /*上 右 下 左*/
  padding: 20px 20px 20px 25px;
  margin: 0 auto;
}

.blog-created {
  font-family: Microsoft YaHei;
  text-align: right;
  color: #af2fff;
}

.r-icon-delete {
  margin-left: 10px;
}
</style>
